﻿using System;
using System.Collections.ObjectModel;
using Microsoft.Practices.Composite.Regions;
using Microsoft.Practices.Unity;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using RAD.Modules.AD.Interfaces;
using RAD.Modules.AD.PresentationModels;
using RAD.Modules.AD.Services;
using RAD.Modules.AD.Tests.Mocks;
using RAD.Modules.AD.Views;

namespace RAD.Modules.AD.Tests
{
    [TestClass]
    public class ForestModuleFixture
    {
        [TestMethod]
        public void RegisterViewsServicesRegistersViewsAndServices()
        {
            MockUnityContainer container = new MockUnityContainer();

            TestableForestModule module = new TestableForestModule(container, new MockRegionManager());

            module.InvokeRegisterViewsAndServices();

            Assert.AreEqual(typeof (NetService), container.Types[typeof (INetService)]);
            Assert.AreEqual(typeof (ADView), container.Types[typeof (IADView)]);
            Assert.AreEqual(typeof (ADPresenter), container.Types[typeof (IADPresenter)]);
        }

        [TestMethod]
        public void InitAddsADViewToADRegion()
        {
            MockRegion ADRegion = new MockRegion();
            MockRegion detailsRegion = new MockRegion();
            MockRegionManager regionManager = new MockRegionManager();
            var container = new MockUnityResolver();
            container.Bag.Add(typeof (IADPresenter), new MockForestPresenter());
            container.Bag.Add(typeof (IDetailsViewPresenter), new MockDetailsPresenter());
            ADModule module = new ADModule(container, regionManager);
            regionManager.Register("ADRegion", ADRegion);
            regionManager.Register("DetailsRegion", detailsRegion);

            Assert.AreEqual(0, ADRegion.AddedViews.Count);

            module.Initialize();

            Assert.AreEqual(1, ADRegion.AddedViews.Count);
        }


        internal class TestableForestModule : ADModule
        {
            public TestableForestModule(IUnityContainer container, IRegionManager regionManager)
                : base(container, regionManager)
            {
            }

            public void InvokeRegisterViewsAndServices()
            {
                base.RegisterViewsAndServices();
            }
        }
    }

    internal class MockForestPresenter : IADPresenter
    {
        #region IADPresenter Members

        private IADView _view = new MockForestView();

        public IADView View
        {
            get { return _view; }
            set { _view = value; }
        }

        public ObservableCollection<ADItem> DomainNamesList
        {
            get { throw new NotImplementedException(); }
        }

        #endregion
    }

    internal class MockDetailsPresenter : IDetailsViewPresenter
    {
        #region IADPresenter Members

        private IDetailsView _view = new MockDetailsView();

        public IDetailsView View
        {
            get { return _view; }
            set { _view = value; }
        }

        public ObservableCollection<ADItem> DomainNamesList
        {
            get { throw new NotImplementedException(); }
        }

        #endregion
    }
}